package it.polimi.traveldream.web.beans.guest;

import it.polimi.traveldream.managers.PacchettoManager;
import it.polimi.traveldream.managers.ProductManager;
import it.polimi.traveldream.managers.credentials.UserCredentialManager;
import it.polimi.traveldream.persistence.dtos.AeroportoDTO;
import it.polimi.traveldream.persistence.dtos.CompagniaDTO;
import it.polimi.traveldream.persistence.dtos.HotelProdDTO;
import it.polimi.traveldream.persistence.dtos.PacchettoDTO;
import it.polimi.traveldream.persistence.dtos.TourProdDTO;
import it.polimi.traveldream.persistence.dtos.TrattaProdDTO;
import it.polimi.traveldream.web.beans.login.XHTMLPagesConstants;

import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;

@ManagedBean(name="showPacPers")
@ViewScoped
public class ShowPacchettoPers {

	@EJB
	private PacchettoManager paccMgr;
	
	@EJB
	private ProductManager productMgr;
	
	@EJB
	private UserCredentialManager userMgr;

	private PacchettoDTO pacchetto;
	
	
	@Email
	@NotEmpty
	private String email;
	
	private String link;
	
	int ID;

	public void preloadData() {
		link="link";
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> parameters = context.getExternalContext().getRequestParameterMap();
		
		try{
		ID = Integer.parseInt(parameters.get("ID"));
		}catch(Exception e){
			
		/*	String message = "Errore";
			FacesContext con = FacesContext.getCurrentInstance();
			con.addMessage(XHTMLPagesConstants.INVITE_FRIEND,
					new FacesMessage(message));*/
		}
		System.out.println("INVITO"+ ID);
		setPacchetto(paccMgr.findPacchetto(ID));
		System.out.println(pacchetto.getID());
	}
	
	public void invito(){
		
	}

	public PacchettoDTO getPacchetto() {
		return pacchetto;
	}

	public void setPacchetto(PacchettoDTO pacchetto) {
		this.pacchetto = pacchetto;
	}
	
	public List<TrattaProdDTO> getVoli(){
		return paccMgr.getTratteTours(pacchetto.getID());
	}
	
	public List<TourProdDTO> getTours(){
		return paccMgr.getPackTours(pacchetto.getID());
	}
	
	public List<HotelProdDTO> getHotels(){
		return paccMgr.getPackHotels(pacchetto.getID());
	}
	
	public void invite() {
		System.out.println(pacchetto.getID());
		link= new StringBuilder().append("localhost:8080/TravelDreamWeb/invite.xhtml?ID=").append(ID).toString();
		System.out.println(link);

	}
	
	public void addInfo(ActionEvent actionEvent) {  
		link= new StringBuilder().append("http://localhost:8080/TravelDreamWeb/invite.xhtml?ID=").append(ID).toString();
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,"Utente Invitato: ", link));  
    }  

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getLink() {
		return link;
	}

	public void setLink(String link) {
		this.link = link;
	}
	
	public AeroportoDTO getAeroporto(int AeroportoID) {
		return productMgr.findAeroporto(AeroportoID);
	}
	
	public CompagniaDTO getCompagnia(int compagniaDTOID){
		return productMgr.findCompagnia(compagniaDTOID);
	}
}